package com.will.reader.chapterList

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RadioButton
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import com.will.reader.base.BaseDialogFragment
import com.will.reader.chapterList.viewmodel.ChapterIndexingViewModel
import com.will.reader.data.model.Chapter
import com.will.reader.databinding.FragmentChapterIndexingBinding
import com.will.reader.viewmodel.AppViewModel
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

/**
 * created  by will on 2020/12/12 18:21
 */
class ChapterIndexingFragment (private val finishCallback: (List<Chapter>) -> Unit): BaseDialogFragment() {

    private val appViewModel: AppViewModel by activityViewModels()
    private val viewModel: ChapterIndexingViewModel by viewModels()
    private lateinit var finder: ChapterFinder

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        finder = ChapterFinder(appViewModel.book().value!!)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
       //isCancelable = false
       val binding = FragmentChapterIndexingBinding.inflate(layoutInflater,container,false)
       initView(binding)
       return binding.root
    }

    private fun initView(binding: FragmentChapterIndexingBinding){
        binding.fragmentChapterIndexProgress.max = 100
        binding.fragmentChapterIndexProgressText.text = "0%"
        binding.fragmentChapterIndexChapterText.text = "未发现章节"

        binding.fragmentChapterIndexSearchButton.setOnClickListener {
            binding.fragmentChapterIndexSearchLayout.visibility = View.INVISIBLE
            binding.fragmentChapterIndexProgressLayout.visibility = View.VISIBLE
            val checkedRadioId = binding.fragmentChapterIndexRadioGroup.checkedRadioButtonId
            val keyword = binding.root.findViewById<RadioButton>(checkedRadioId).text.toString()
            isCancelable = false
            viewLifecycleOwner.lifecycleScope.launch{
                finder.indexing(keyword).collectLatest {
                    when(it){
                        is ChapterFinder.FindResult.Find -> {
                            binding.fragmentChapterIndexChapterText.text = it.chapter.name
                            viewModel.addChapter(it.chapter)
                        }
                        is ChapterFinder.FindResult.Progress -> {
                            binding.fragmentChapterIndexProgress.progress = it.progress
                            binding.fragmentChapterIndexProgressText.text = "${it.progress}%"
                        }
                        is ChapterFinder.FindResult.Finish -> {
                            binding.fragmentChapterIndexProgressText.text = "检索完毕"
                            binding.fragmentChapterIndexProgress.progress = 100
                            finishCallback.invoke(viewModel.getChapters())
                            dismiss()
                        }
                    }

                }
            }
        }
        binding.fragmentChapterIndexCancelButton.setOnClickListener {
            dismiss()
        }
    }

}